package main;

import main.components.*;
import main.objects.*;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

/**
* Created by s_shmakov on 25.07.14.
*/
public class SetActualSmet extends JDialog{

    private JLabel headerLabel;
    private SmetNode actualSmet, newSmet;
    private JTable newTable, actualTable;
    private JSplitPane jSplitPane;
    private JScrollPane newTableScroll, actualTableScroll;
    private ArrayList<SmetNode> newSmetList, actualSmetList;
    private HashMap<Integer, HashSet<Integer>> listChanges;
    private HashMap<Integer, SmetNode> actualSmetHash, newSmetHash;
    private HashSet<Integer> hashSetChanges;
    private HashMap<Integer, Boolean> addRemoveRow;

    public SetActualSmet(SmetNode actualSmet, SmetNode newSmet, ArrayList<SmetNode> newSmetList, ArrayList<SmetNode> actualSmetList) {
        this.actualSmet = actualSmet;
        this.newSmet = newSmet;
        this.newSmetList = newSmetList;
        this.actualSmetList = actualSmetList;
        listChanges = new HashMap<>();
    }

    public void showDialog() {

        initData();

        Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
        int width = screensize.width - 100;
        int height = screensize.height - 100;

        setSize(new Dimension(width, height));
        setLocation(screensize.width / 2 - width / 2, screensize.height / 2 - height / 2);
        setLayout(new GridBagLayout());
        setModal(true);

        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

        GradientPanel headerPanel = new GradientPanel(new GridBagLayout(), new Color(252, 141, 234), new Color(178, 99, 164));
        headerLabel = new JLabel("Загружаемая смета уже есть в базе: " + (listChanges.values().size() < 1 ? " показатели без изменений." : "изменено строк с показателями: " + listChanges.values().size() + "." ));

        headerLabel.setFont(new Font("Segoe UI", 0, 18));
        headerLabel.setForeground(Color.WHITE);

        add(headerPanel, new GridBagConstraints(0, 0, 4, 1, 0.1, 0.1, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
        headerPanel.add(headerLabel, new GridBagConstraints(0, 0, 1, 1, 0.5, 0, GridBagConstraints.WEST, GridBagConstraints.WEST, new Insets(30, 56, 7, 56), 0, 0));

        SmrTableModel newSmrTableModel = new SmrTableModel(newSmetList);
        newTable = new JTable(newSmrTableModel);
        newTableScroll = new JScrollPane(newTable);

        actualTable = new JTable(new SmrTableModel(actualSmetList));
        actualTableScroll = new JScrollPane(actualTable);

        jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, newTableScroll, actualTableScroll);
        jSplitPane.setDividerLocation(width / 2);
        jSplitPane.setDividerLocation(0.5);
        jSplitPane.setResizeWeight(0.5);

        newTable.getColumnModel().getColumn(0).setMinWidth(40);
        newTable.getColumnModel().getColumn(0).setMaxWidth(50);
        newTable.getColumnModel().getColumn(1).setMinWidth(250);
        newTable.getColumnModel().getColumn(1).setMaxWidth(350);
        newTable.getColumnModel().getColumn(2).setMinWidth(110);
        newTable.getColumnModel().getColumn(2).setMaxWidth(150);
        actualTable.getColumnModel().getColumn(0).setMinWidth(40);
        actualTable.getColumnModel().getColumn(0).setMaxWidth(50);
        actualTable.getColumnModel().getColumn(1).setMinWidth(250);
        actualTable.getColumnModel().getColumn(1).setMaxWidth(350);
        actualTable.getColumnModel().getColumn(2).setMinWidth(110);
        actualTable.getColumnModel().getColumn(2).setMaxWidth(150);

        for (int i = 0; i < 9; i++) {
            if (i < 1 && i > 2) {
                newTable.getColumnModel().getColumn(i).setCellRenderer(new ChangeCellRenderer());
                actualTable.getColumnModel().getColumn(i).setCellRenderer(new ChangeCellRenderer());
            } else {
                newTable.getColumnModel().getColumn(i).setCellRenderer(new CellRenderer());
                actualTable.getColumnModel().getColumn(i).setCellRenderer(new CellRenderer());
            }
        }

        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        panel.add(new JLabel("Загружаемая смета:"), new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.WEST, new Insets(0, 10, 0, 0), 0, 0));
        panel.add(new JLabel("Текущая смета в базе:"), new GridBagConstraints(1, 0, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.WEST, new Insets(0, 10, 0, 0), 0, 0));
        add(panel, new GridBagConstraints(0, 1, 4, 1, 10, 0.1, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(28, 56, 0, 56), 0, 0));

        add(jSplitPane, new GridBagConstraints(0, 2, 4, 1, 10, 10, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0, 56, 0, 56), 0, 0));

        add(new JLabel(), new GridBagConstraints(0, 3, 1, 1, 10, 0.1, GridBagConstraints.EAST, GridBagConstraints.EAST, new Insets(18, 10, 36, 0), 0, 0));

        JButton addChooseButton = new JButton("Добавить версию");
        add(addChooseButton, new GridBagConstraints(1, 3, 1, 1, 0, 0.1, GridBagConstraints.EAST, GridBagConstraints.EAST, new Insets(18, 10, 36, 0), 0, 0));
        addChooseButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {

                FactageSmet.IMPORT_FACTAGE_CONNECTION = MsSQLConnect.ConnectDB("ImportFactage");

                try {
                    Statement stat = FactageSmet.IMPORT_FACTAGE_CONNECTION.createStatement();

                    //region Обновляем старую смету
                    stat.executeUpdate("update Smet set status = 'Отменена' WHERE id = " + actualSmet.getId());
                    actualSmet.setStatus("Отменена");
                    //endregion

                    //region Обновляем новую смету
                    newSmet.setChange(actualSmet.getChange() + 1);
                    stat.executeUpdate("update Smet set change = " + newSmet.getChange() + " WHERE id = " + newSmet.getId());
                    //endregion

                    //region Старые связи не актуальны
                    stat.executeUpdate("update [Factage].[dbo].[link]\n" +
                            "\tset actual = 0\n" +
                            "\tfrom [Factage].[dbo].[link] as l\n" +
                            "\tleft join [importFactage].[dbo].[Smr] as s on s.id = l.smr_id and l.importFactage = 'true'\n" +
                            "\twhere s.smet_id = " + actualSmet.getId());
                    //endregion

                    //region Копируем новые связи
                    stat.executeUpdate("insert into [Factage].[dbo].[link] (task_id, smr_id, smr_name, smr_num, sm_num, smr_vol, link_factor, link_full_factor, pv_flag, pv_vol, pv_tr_id, mech_vol, mech_tr_id, t_vol, t_tr_id, mr_vol, mr_tr_id, db_name, proj_id, test,[smr_unit],[actual_pv],[importFactage], actual)\n" +
                            "select \n" +
                            "\t\tl.task_id,\n" +
                            "\t\ts.id,\n" +
                            "\t\ts.name, \n" +
                            "\t\ts.num, \n" +
                            "\t\tsmet.num, \n" +
                            "\t\ts.volume, \n" +
                            "\t\tl.link_factor, \n" +
                            "\t\tl.link_full_factor, \n" +
                            "\t\tl.pv_flag, \n" +
                            "\t\tcase\n" +
                            "\t\t\twhen s.unit like '%1000%'\n" +
                            "\t\t\tthen\n" +
                            "\t\t\t\ts.volume * l.link_factor / l.link_full_factor * 1000\n" +
                            "\t\t\telse \n" +
                            "\t\t\t\tcase\n" +
                            "\t\t\t\t\twhen s.unit like '%100%'\n" +
                            "\t\t\t\t\tthen\n" +
                            "\t\t\t\t\t\ts.volume * l.link_factor / l.link_full_factor * 100\n" +
                            "\t\t\t\t\telse \n" +
                            "\t\t\t\t\t\tcase\n" +
                            "\t\t\t\t\t\t\twhen s.unit like '%10%'\n" +
                            "\t\t\t\t\t\t\tthen\n" +
                            "\t\t\t\t\t\t\t\ts.volume * l.link_factor / l.link_full_factor * 10\n" +
                            "\t\t\t\t\t\t\telse \n" +
                            "\t\t\t\t\t\t\t\ts.volume * l.link_factor / l.link_full_factor\n" +
                            "\t\t\t\t\t\tend\n" +
                            "\t\t\t\tend\n" +
                            "\t\tend as pv_vol, " +
                            "\t\tl.pv_tr_id, \n" +
                            "\t\ts.mech * l.link_factor / l.link_full_factor, \n" +
                            "\t\tl.mech_tr_id, \n" +
                            "\t\ts.t * l.link_factor / l.link_full_factor, \n" +
                            "\t\tl.t_tr_id, \n" +
                            "\t\ts.mr * l.link_factor / l.link_full_factor, \n" +
                            "\t\tl.mr_tr_id, \n" +
                            "\t\tl.db_name, \n" +
                            "\t\tl.proj_id, \n" +
                            "\t\tl.test,\n" +
                            "\t\ts.[unit],\n" +
                            "\t\tl.[actual_pv],\n" +
                            "\t\tl.[importFactage], \n" +
                            "\t\t1\n" +
                            "\tfrom [Factage].[dbo].[link] as l\n" +
                            "\tinner join [importFactage].[dbo].[Smr] as sm on sm.id = l.smr_id and sm.smet_id = " + actualSmet.getId() + "\n" +
                            "\tinner join [importFactage].[dbo].[Smr] as s on s.nauka_id = sm.nauka_id and s.smet_id = " + newSmet.getId() + "\n" +
                            "\tinner join [importFactage].[dbo].[smet] as smet on s.smet_id = smet.id\n" +
                            "\twhere s.id is not null");
                    //endregion

                    stat.close();

                    FactageSmet.mF.treeSmet.reloadModel();

                    FactageSmet.IMPORT_FACTAGE_CONNECTION.close();

                } catch (SQLException e) {
                    e.printStackTrace();
                }
                dispose();
            }
        });


        JButton replaceChooseButton = new JButton("Заменить");
        add(replaceChooseButton, new GridBagConstraints(2, 3, 1, 1, 0, 0.1, GridBagConstraints.EAST, GridBagConstraints.EAST, new Insets(18, 10, 36, 0), 0, 0));
        replaceChooseButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {

                int dialogButton = JOptionPane.YES_NO_OPTION;
                int dialogResult = JOptionPane.showConfirmDialog (null, "Вы действительно хотите заменить смету \"" + actualSmet.getNum() + "\" и перенести связи на новую? ","Замена", dialogButton);
                if (dialogResult != JOptionPane.YES_OPTION) return;

                FactageSmet.IMPORT_FACTAGE_CONNECTION = MsSQLConnect.ConnectDB("ImportFactage");

                try {

                    Statement stat = FactageSmet.IMPORT_FACTAGE_CONNECTION.createStatement();

                    //region Копируем новые связи
                    stat.executeUpdate("insert into [Factage].[dbo].[link] (task_id, smr_id, smr_name, smr_num, sm_num, smr_vol, link_factor, link_full_factor, pv_flag, pv_vol, pv_tr_id, mech_vol, mech_tr_id, t_vol, t_tr_id, mr_vol, mr_tr_id, db_name, proj_id, test,[smr_unit],[actual_pv],[importFactage], actual)\n" +
                            "select \n" +
                            "\t\tl.task_id,\n" +
                            "\t\ts.id,\n" +
                            "\t\ts.name, \n" +
                            "\t\ts.num, \n" +
                            "\t\tsmet.num, \n" +
                            "\t\ts.volume, \n" +
                            "\t\tl.link_factor, \n" +
                            "\t\tl.link_full_factor, \n" +
                            "\t\tl.pv_flag, \n" +
                            "\t\tcase\n" +
                            "\t\t\twhen s.unit like '%1000%'\n" +
                            "\t\t\tthen\n" +
                            "\t\t\t\ts.volume * l.link_factor / l.link_full_factor * 1000\n" +
                            "\t\t\telse \n" +
                            "\t\t\t\tcase\n" +
                            "\t\t\t\t\twhen s.unit like '%100%'\n" +
                            "\t\t\t\t\tthen\n" +
                            "\t\t\t\t\t\ts.volume * l.link_factor / l.link_full_factor * 100\n" +
                            "\t\t\t\t\telse \n" +
                            "\t\t\t\t\t\tcase\n" +
                            "\t\t\t\t\t\t\twhen s.unit like '%10%'\n" +
                            "\t\t\t\t\t\t\tthen\n" +
                            "\t\t\t\t\t\t\t\ts.volume * l.link_factor / l.link_full_factor * 10\n" +
                            "\t\t\t\t\t\t\telse \n" +
                            "\t\t\t\t\t\t\t\ts.volume * l.link_factor / l.link_full_factor\n" +
                            "\t\t\t\t\t\tend\n" +
                            "\t\t\t\tend\n" +
                            "\t\tend as pv_vol, " +
                            "\t\tl.pv_tr_id, \n" +
                            "\t\ts.mech * l.link_factor / l.link_full_factor, \n" +
                            "\t\tl.mech_tr_id, \n" +
                            "\t\ts.t * l.link_factor / l.link_full_factor, \n" +
                            "\t\tl.t_tr_id, \n" +
                            "\t\ts.mr * l.link_factor / l.link_full_factor, \n" +
                            "\t\tl.mr_tr_id, \n" +
                            "\t\tl.db_name, \n" +
                            "\t\tl.proj_id, \n" +
                            "\t\tl.test,\n" +
                            "\t\ts.[unit],\n" +
                            "\t\tl.[actual_pv],\n" +
                            "\t\tl.[importFactage], \n" +
                            "\t\t1\n" +
                            "\tfrom [Factage].[dbo].[link] as l\n" +
                            "\tinner join [importFactage].[dbo].[Smr] as sm on sm.id = l.smr_id and sm.smet_id = " + actualSmet.getId() + "\n" +
                            "\tinner join [importFactage].[dbo].[Smr] as s on s.nauka_id = sm.nauka_id and s.smet_id = " + newSmet.getId() + "\n" +
                            "\tinner join [importFactage].[dbo].[smet] as smet on s.smet_id = smet.id\n" +
                            "\twhere s.id is not null");
                    //endregion

                    //region Удаляем старые связи
                    stat.executeUpdate("DELETE \n" +
                            "\tFROM [Factage].[dbo].[link] \n" +
                            "\tWHERE smr_id IN (SELECT id FROM [importFactage].[dbo].[Smr] WHERE [importFactage].[dbo].[Smr].smet_id = " + actualSmet.getId() + ")");
                    //endregion

                    //region Удаляем и прячем старую смету
                    stat.executeUpdate("DELETE FROM Smet WHERE id = " + newSmet.getId());
                    stat.close();

                    newSmet.removeAllChildren();
                    MainFrame.ALL_EPS.get(newSmet.getEpsId() + "-eps").getSmets().remove(newSmet);

                    MainFrame.ALL_SMET.get(newSmet.getId() + "-smet").removeAllChildren();
                    MainFrame.ALL_SMET.get(newSmet.getId() + "-smet").removeFromParent();
                    MainFrame.ALL_SMET.remove(newSmet.getId() + "-smet");

                    FactageSmet.mF.treeSmet.reloadModel();

                    FactageSmet.mF.treeSmet.reloadModel();
                    //endregion

                    FactageSmet.IMPORT_FACTAGE_CONNECTION.close();

                } catch (SQLException e) {
                    e.printStackTrace();
                }
                dispose();
            }
        });


        JButton cancelChooseButton = new JButton("Отмена");
        add(cancelChooseButton, new GridBagConstraints(3, 3, 1, 1, 0, 0.1, GridBagConstraints.EAST, GridBagConstraints.EAST, new Insets(18, 10, 36, 56), 0, 0));
        cancelChooseButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                FactageSmet.IMPORT_FACTAGE_CONNECTION = MsSQLConnect.ConnectDB("ImportFactage");

                try {
                    Statement stat = FactageSmet.IMPORT_FACTAGE_CONNECTION.createStatement();
                    stat.executeUpdate("DELETE FROM Smet WHERE id = " + newSmet.getId());
                    stat.close();

                    newSmet.removeAllChildren();
                    MainFrame.ALL_EPS.get(newSmet.getEpsId() + "-eps").getSmets().remove(newSmet);

                    MainFrame.ALL_SMET.get(newSmet.getId() + "-smet").removeAllChildren();
                    MainFrame.ALL_SMET.get(newSmet.getId() + "-smet").removeFromParent();
                    MainFrame.ALL_SMET.remove(newSmet.getId() + "-smet");

                    FactageSmet.mF.treeSmet.reloadModel();

                    FactageSmet.IMPORT_FACTAGE_CONNECTION.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                dispose();
            }
        });
        setAddRemoveRow();

        int added = 0;
        int deleted = 0;

        for (boolean check : addRemoveRow.values()) {
            if (check) added++;
            else deleted ++;
        }

        if (listChanges.values().size() < 1 && addRemoveRow.values().size() < 1) {
            addChooseButton.setEnabled(false);
            replaceChooseButton.setEnabled(false);
        }

        headerLabel.setText(headerLabel.getText() + (added > 0 ? " Добавлено строк: " + added  : "") + (deleted > 0 ? " Удалено строк: " + deleted : "") );

        setVisible(true);

    }

    public void setAddRemoveRow() {

        addRemoveRow = new HashMap<>();
        for (int i = 0; i < newTable.getModel().getRowCount(); i++) {
            if (listChanges.containsKey((Integer) newTable.getModel().getValueAt(i, 0))) {
                hashSetChanges.add(i);
            }
            int numRow = (Integer) newTable.getModel().getValueAt(i, 0);
            if (!actualSmetHash.containsKey(numRow)) {
                addRemoveRow.put(i, true);
            }
        }
        for (int i = 0; i < actualTable.getModel().getRowCount(); i++) {
            int numRow = (Integer) actualTable.getModel().getValueAt(i, 0);
            if (!newSmetHash.containsKey(numRow)) {
                addRemoveRow.put(i, false);
            }
        }

    }

    public void initData() {

        actualSmetHash = new HashMap<>();
        newSmetHash = new HashMap<>();
        hashSetChanges = new HashSet<>();

        for (SmetNode actSmet : actualSmetList) {
            actualSmetHash.put(actSmet.getSmrNum(), actSmet);
        }

        for (SmetNode newSmet : newSmetList) {
            newSmetHash.put(newSmet.getSmrNum(), newSmet);
        }

//        for (SmetNode newSmet : newSmetList) {
//            if (newSmet.getName() != null && actualSmetHash.containsKey(newSmet.getSmrNum())) {
//                if (listChanges.get(newSmet.getId()) != null) {
//                    if (!newSmet.getName().equals(actualSmetHash.get(newSmet.getSmrNum()).getName())) {
//                        listChanges.get(newSmet.getSmrNum()).add(1);
//                    } else if (!newSmet.getCode().equals(actualSmetHash.get(newSmet.getSmrNum()).getCode())) {
//                        listChanges.get(newSmet.getSmrNum()).add(2);
//                    } else if (!newSmet.getUnit().equals(actualSmetHash.get(newSmet.getSmrNum()).getUnit())) {
//                        listChanges.get(newSmet.getSmrNum()).add(3);
//                    } else if (!newSmet.getVolume().equals(actualSmetHash.get(newSmet.getSmrNum()).getVolume())) {
//                        listChanges.get(newSmet.getSmrNum()).add(4);
//                    } else if (!newSmet.getT().equals(actualSmetHash.get(newSmet.getSmrNum()).getT())) {
//                        listChanges.get(newSmet.getSmrNum()).add(5);
//                    } else if (!newSmet.getMech().equals(actualSmetHash.get(newSmet.getSmrNum()).getMech())) {
//                        listChanges.get(newSmet.getSmrNum()).add(6);
//                    } else if (!newSmet.getMr().equals(actualSmetHash.get(newSmet.getSmrNum()).getMr())) {
//                        listChanges.get(newSmet.getSmrNum()).add(7);
//                    }
//                } else {
//                    HashSet<Integer> tempSet = new HashSet<>();
//                    if (!newSmet.getName().equals(actualSmetHash.get(newSmet.getSmrNum()).getName())) {
//                        tempSet.add(1);
//                        listChanges.put(newSmet.getSmrNum(), tempSet);
//                    } else if (!newSmet.getCode().equals(actualSmetHash.get(newSmet.getSmrNum()).getCode())) {
//                        tempSet.add(2);
//                        listChanges.put(newSmet.getSmrNum(), tempSet);
//                    } else if (!newSmet.getUnit().equals(actualSmetHash.get(newSmet.getSmrNum()).getUnit())) {
//                        tempSet.add(3);
//                        listChanges.put(newSmet.getSmrNum(), tempSet);
//                    } else if (!newSmet.getVolume().equals(actualSmetHash.get(newSmet.getSmrNum()).getVolume())) {
//                        tempSet.add(4);
//                        listChanges.put(newSmet.getSmrNum(), tempSet);
//                    } else if (!newSmet.getT().equals(actualSmetHash.get(newSmet.getSmrNum()).getT())) {
//                        tempSet.add(5);
//                        listChanges.put(newSmet.getSmrNum(), tempSet);
//                    } else if (!newSmet.getMech().equals(actualSmetHash.get(newSmet.getSmrNum()).getMech())) {
//                        tempSet.add(6);
//                        listChanges.put(newSmet.getSmrNum(), tempSet);
//                    } else if (!newSmet.getMr().equals(actualSmetHash.get(newSmet.getSmrNum()).getMr())) {
//                        tempSet.add(7);
//                        listChanges.put(newSmet.getSmrNum(), tempSet);
//                    }
//                }
//            }
//        }
    }

    class ChangeCellRenderer extends DefaultTableCellRenderer {

        public ChangeCellRenderer() {

        }

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            super.setBorder(new EmptyBorder(new Insets(1, 3, 1, 3)));
            if (table != actualTable) {
                if (hashSetChanges.contains(row)) {
                    super.setBackground(isSelected ? new Color(255, 195, 128) : new Color(255, 255, 198));
                } else {
                    super.setBackground(isSelected ? new Color(227, 227, 227) : Color.WHITE);
                }
                if (addRemoveRow.containsKey(row) && addRemoveRow.get(row)) {
                    super.setBackground(isSelected ? new Color(122, 207, 255) : new Color(198, 223, 255));
                }
            } else {
                if (addRemoveRow.containsKey(row) && addRemoveRow.get(row) == false) {
                    super.setBackground(isSelected ? new Color(255, 114, 105) : new Color(255, 161, 156));
                } else {
                    super.setBackground(isSelected ? new Color(227, 227, 227) : Color.WHITE);
                }
            }

            super.setForeground(isSelected ? Color.black : Color.black);
            super.setText(value != null ? value.toString() : "");
            super.setFont(new Font("Calibri", 0, 12));

            return this;
        }

    }

    class CellRenderer extends JTextArea implements TableCellRenderer {

        private static final long serialVersionUID = 1L;

        private CellRenderer() {

            super();
            setLineWrap(true);
            setWrapStyleWord(true);
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object arg1, boolean isSelected, boolean hasFocus, int row, int column) {
                if (table != actualTable) {
                    if (hashSetChanges.contains(row)) {
                        super.setBackground(isSelected ? new Color(255, 195, 128) : new Color(255, 255, 198));
                    } else {
                        super.setBackground(isSelected ? new Color(227, 227, 227) : Color.WHITE);
                    }
                    if (addRemoveRow.containsKey(row) && addRemoveRow.get(row)) {
                        super.setBackground(isSelected ? new Color(122, 207, 255) : new Color(198, 223, 255));
                    }
                } else {
                    if (addRemoveRow.containsKey(row) && !addRemoveRow.get(row)) {
                        super.setBackground(isSelected ? new Color(255, 114, 105) : new Color(255, 161, 156));
                    } else {
                        super.setBackground(isSelected ? new Color(227, 227, 227) : Color.WHITE);
                    }
                }


            String data = (String) arg1.toString();
            super.setFont(new Font("Calibri", 0, 12));
            int lineWidth = this.getFontMetrics(this.getFont()).stringWidth(data);
            int lineHeight = this.getFontMetrics(this.getFont()).getHeight();
            int rowWidth = table.getCellRect(row, column, true).width;

            int newRowHeight = (int) ((lineWidth / rowWidth) * (lineHeight)) + lineHeight * 2;
            if (table.getRowHeight(row) != newRowHeight) {
                table.setRowHeight(row, newRowHeight);
            }
            this.setText(data);
            return this;
        }
    }

}
